package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5991
 * http://www.mingxing.com/  明星网
 *
 * @author KK
 *
 */
public class MingxingNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/

		"http://news.mingxing.com/.*/[0-9]{8,}.html",
		"http://news.mingxing.com/read/[0-9]*/[0-9]*.html",
		"http://tuku.mingxing.com/[a-z]*/[0-9]*/1.html",
		"http://www.mingxing.com/news/[a-z0-9\\/]+.html"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		if(doc.select("div#logo").size()>0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		news.setUrl(url);

		title =doc.select("title").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("h1.art_title").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("h1").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("div.reads h1").text();
				}
			}
		}
		if (title!=null)title=StringUtils.substringBefore(title, "-");
		content=HtmlCleaner.getContentHtml(url,doc.select("div.content,p.descript"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.box02"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.intro"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.contbox"));
				}
			}
		}
		date=DateUtils.matchDate(doc.select("div.info,p.info").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("span.txt01").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div.intro").text());
				if(date == null){
					date=DateUtils.matchDate(doc.select("div.info").text());
				}
			}
		}
		if(date == null){
			date=JSoupUtils.matchDate(doc, "发布于：");
		}
		String author=JSoupUtils.matchAuthor(doc, "来源:");
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);
			news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://www.mingxing.com/news/baoliaotai/99/294133001.html";
		MingxingNewsAnalyse analyse=new MingxingNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
